package com.sansi.pencilbook.mapper;

import com.sansi.pencilbook.entity.Category;
import org.apache.ibatis.annotations.*;

import java.sql.SQLException;
import java.util.List;

/**
 * 功能说明: 类别的增删改查
 *
 * @author lsy13
 */
@Mapper
public interface CategoryMapper {

    @Select("select category_id, category_name, category_parent from category")
    public List<Category> findAll() throws SQLException;

    @Select("select category_id, category_name, category_parent from category where category_parent=#{parentId}")
    public List<Category> findByParent(int parentId) throws SQLException;

    @Select("select category_id, category_name, category_parent from category where category_name=#{name}")
    public Category findByName(String name) throws SQLException;

    @Select("select category_id, category_name, category_parent from category where category_id=#{id}")
    public Category findById(int id) throws SQLException;

    @Insert("insert into category (category_id, category_name, category_parent) " +
            "values(#{categoryId},#{categoryName},#{categoryParent})")
    public int saveCategory(Category category) throws SQLException;

    @Update("update category set category_name=#{categoryName}, category_parent=#{categoryParent} " +
            "where category_id=#{categoryId}")
    public int updateCategory(Category category) throws SQLException;

    @Delete("delete from category where category_id=#{categoryId}")
    public int deleteCategory(Category category) throws SQLException;

    @Delete("<script>" +
            "delete from category where category_id in " +
            "<foreach collection='categories' item='category' index='index' open='(' separator=',' close=')'>" +
            "#{category.categoryId}" +
            "</foreach>" +
            "</script>")
    public int deleteCategories(List<Category> categories) throws SQLException;
}
